import { getToken } from '@/utils'
import { createRouter, createWebHashHistory, type Router } from 'vue-router'
import { BASE_HOME_URL } from '@/config'
import type { App } from 'vue'

const router:Router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: BASE_HOME_URL,
      component: () => import('../components/layout/base-layout.vue'),
      children: [
        {
          path: 'dashboard',
          name: 'dashboard',
          meta: {
            menu: true,
            title: '首页',
            icon: 'Home'
          },
          component: () => import('../views/dashboard/dashboard.vue')
        },
        {
          path: 'font',
          name: 'font',
          meta: {
            menu: true,
            title: '我的字体',
            icon: 'FileText'
          },
          component: () => import('../views/font-admin/font-admin.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/login.vue')
    },

    {
      name: 'editor',
      path: '/editor',
      component: () => import('../views/graph-editor/graph-editor.vue')
    }
  ]
})
router.beforeEach((to) => {
  const token = getToken()
  if (!token && to.name !== 'login') {
    return {
      name: 'login'
    }
  }
  if (to.name === 'editor') {
    const loadingPage = document.querySelector('#loading-page') as HTMLElement
    loadingPage.style.display = 'flex'
  }
})
export function setupRouter (app:App) {
  app.use(router)
}

export default router
